import requests


# 参考链接  上传文件类型： https://blog.csdn.net/weixin_43648947/article/details/89216622

# requests上传图片/文件
def get_img():
    with open('/Users/jxl/Desktop/5.jpg', 'rb') as f:
        img = f.read()
    # file = {'profile_picture': ('img.jpg', img, 'image/jpg')}  # profile_picture为字段名  不能修改
    file = {'profile_picture': ('4.jpg', img)}
    return file


with requests.Session() as session:
    URL = 'http://47.103.89.53:8080/users/activeuser/1/'
    session.auth = ('admin', 'admin123')   # 认证

    # 上传文件
    file = get_img()
    response = session.put(url=URL, files=file)  # timeout响应超时
    print(response.json())

    # r = session.get(URL, cookies={'from-my': 'browser'})  # get请求，发送cookies

    # 请求图片保存本地
    PICTURE_URL = 'http://47.103.89.53:8080/media/portrait/4.jpg/'
    r1 = session.get(url=PICTURE_URL)
    with open('/Users/jxl/Desktop/6.jpg', 'wb+') as f:
        f.write(r1.content)

# get请求传参
"""
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

>>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3

r.content  二进制文件 bytes
r.status_code 响应状态码
r.json()  响应数据
r.text
"""

# 请求代理
"""
proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)
"""



# post请求json传参
"""
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post(url, json=payload)
"""



# 请求头
"""
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}  # dict

>>> r = requests.get(url, headers=headers)
"""


